package com.example.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yuejian.Adapter.CommentAdapter;
import com.example.yuejian.Bean.PostComment;
import com.example.yuejian.Bean.Success;
import com.example.yuejian.R;
import com.example.yuejian.listener.ClubListener;
import com.example.yuejian.model.CommentModel;
import com.squareup.picasso.Picasso;

import java.util.List;


public class PostDetail extends AppCompatActivity implements View.OnClickListener,
        ClubListener<PostComment>{

    private ImageView back,img;
    private TextView intro,time,comment;
    private EditText greenhat;
    private String content;
    private RecyclerView recyclerView_comment;
    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);
        initview();
        initEvent();
    }
    private void initview(){
        Intent  intent = getIntent();
         bundle = intent.getExtras();
        CommentModel commentModel = new CommentModel();
        commentModel.clubData(this,Integer.parseInt(bundle.getString("userid")),
                Integer.parseInt(bundle.getString("postid")));
        back = (ImageView)findViewById(R.id.back);
        img = (ImageView)findViewById(R.id.img);
        Picasso.with(this).load(bundle.getString("pic")).into(img);
        intro = (TextView)findViewById(R.id.intro);
        intro.setText(bundle.getString("intro"));
        time = (TextView)findViewById(R.id.time);
        time.setText(bundle.getString("time"));
        comment = (TextView)findViewById(R.id.comment);
        greenhat= (EditText) findViewById(R.id.greenhat);
        content = greenhat.getText().toString();
        recyclerView_comment = (RecyclerView)findViewById(R.id.recyclerView_comment);
        recyclerView_comment.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initEvent(){
        back.setOnClickListener(this);
        comment.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                onBackPressed();
                break;
            case R.id.comment:
                Comment comment = new Comment(this);
                comment.addComment();
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(PostComment postComment) {
        List<PostComment.ComListBean> clublistBeen = postComment.getComList();
        CommentAdapter adapter = new CommentAdapter(this,clublistBeen);
        recyclerView_comment.setAdapter(adapter);
    }

    @Override
    public void onFail(String msg) {

    }



  class  Comment  implements ClubListener<Success> {
    private Context context;
      public Comment(Context context){

      }
      @Override
      public void onResponse(Success success) {
          if (success.getSuccess()==1){
              greenhat.setText("");
          }else{
          }
      }

      @Override
      public void onFail(String msg) {
      }

      private void addComment(){
          String comm = greenhat.getText().toString();
          CommentModel commentModel = new CommentModel();
          commentModel.addcommData(this,Integer.parseInt(bundle.getString("postid")),Integer.parseInt(bundle.getString("userid")),comm);

      }
  }

}
